!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==================================================================================================================================|
subroutine model_resis (et, ro, mx, my, mz, bx, by, bz, en, jx, jy, jz, x, y, nx, ny)
!==================================================================================================================================|

   use parameters
   implicit none

   integer(4), intent(in) :: nx, ny
   real(8), intent(in) :: x(nx), y(ny)
   real(8), dimension(nx, ny), intent(in) :: ro, mx, my, mz, bx, by, bz, en, jx, jy, jz
   real(8), dimension(nx, ny), intent(inout) :: et

   integer(4) :: i, j

   real(8) :: draft_velocity_thr
   real(8) :: et_center_x
   real(8) :: et_center_y
   real(8) :: et_length_x
   real(8) :: et_length_y
   real(8) :: et_factor
   real(8) :: et_draft_max
   real(8) :: et_draft_t_start
   real(8) :: j2, et_draft, draft_velocity, et0

!----------------------------------------------------------------------------------------------------------------------------------|
!  resistivity model
!----------------------------------------------------------------------------------------------------------------------------------|
   et0 = 1.0d-3
   draft_velocity_thr = 1.0d3
   et_center_x = 0.0d0
   et_center_y = 0.0d0
   et_length_x = 0.05d0 * rangex
   et_length_y = 0.05d0 * rangey
   et_factor = 0.01d0
   et_draft_max = 1.0d0
   et_draft_t_start = 0.1d10

   do j = 1, ny
   do i = 1, nx

      if ( abs(x(i) - et_center_x) .le. et_length_x .and. abs(y(j) - et_center_y) .le. et_length_y) then

         j2 = jx(i, j) * jx(i, j) + jy(i, j) * jy(i, j) + jz(i, j) * jz(i, j)

         if (t .gt. et_draft_t_start) then
            draft_velocity = sqrt (j2) / ro(i, j)
            et_draft = max (et_factor * (draft_velocity / draft_velocity_thr - 1.d0), 0.0d0)
            et(i, j) = min (et_draft, et_draft_max)
         else
            et(i, j) = et0 * (cos((x(i) - et_center_x) * pi / et_length_x) + 1.0d0) *                                              &
               (cos((y(j) - et_center_y) * pi / et_length_y) + 1.0d0) / 4.0d0
         endif

      endif

   enddo
   enddo

!----------------------------------------------------------------------------------------------------------------------------------|
   return
end
